<?php

/**
 * CoverController
 * 
 * @author MF
 * @version 2.0
 */

require_once 'Zend/Controller/Action.php';

class Cms_CoverController extends Zend_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		$main = MF_Main::getInstance();
		$coverItem = $main->Cover->getById($id);
		
		$this->view->cover = $coverItem;
		$this->view->image_mode = !$coverItem->file ? 'no_image' : 'cover_image';
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
				
			if (isset($post_data['upload_new'])) {
				$upload_adapter = new Zend_File_Transfer_Adapter_Http();
				$upload_adapter->setDestination($main->_config->general->temp_path);
				if ($upload_adapter->receive()) {
					try {
						$uploaded_file = $main->Utils->Image($upload_adapter->getFileName('im_file'));
						//$uploaded_file->Move($main->_config->general->temp_path);
						$uploaded_file->name = rand(0,40000).'.'.$uploaded_file->extention;
						$this->view->temp_name = $uploaded_file->name;
						$this->view->image_mode = 'temp_image';
					} catch (Exception $e) {
						$this->view->upload_error = $e->getUserMessage();
					}
				}
			} elseif (isset($post_data['uploaded_file'])) {
				try {
					$coverItem->file_name = $main->Utils->fixDirPath($main->_config->general->temp_path).$post_data['uploaded_file'];
					$this->renderScript('exit.phtml');
				} catch (Exception $e) {
					$this->view->error_message = $e->getUserMessage();
					$this->renderScript('fatal.phtml');
				}
			}
			
		}
		
	}
	
	public function tempAction() {
		CmsAuth::check();
		
		if (NULL === $file = $this->getRequest()->getParam('file')) {
			throw new Zend_Controller_Action_Exception('Required param "File"', 404);
		}
		
		try {
			$main = MF_Main::getInstance();
			$uploaded_thumb = $main->Utils->Image($main->Utils->fixDirPath($main->_config->general->temp_path).$file)->Thumb();
			if ($uploaded_thumb->width > $uploaded_thumb->height) {
				if ($uploaded_thumb->width>180)
					$uploaded_thumb->width = 180;
			} else {
				if ($uploaded_thumb->height>180)
					$uploaded_thumb->height = 180;
			}
			$uploaded_thumb->Push();
		} catch (Exception $e) {
			throw new Zend_Controller_Action_Exception($e->getUserMessage(), 404);
		}
		
		$this->_helper->layout->disableLayout();
	}
	
	public function thumbAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$thumb = MF_Main::getInstance()->Cover->getById($id)->file->Thumb();
		if ($thumb->width > $thumb->height) {
			if ($thumb->width>180)
				$thumb->width = 180;
		} else {
			if ($thumb->height>180)
				$thumb->height = 180;
		}
		
		$thumb->Push();
		
		$this->_helper->layout->disableLayout();
	}
	

	public function filledAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$this->view->result = !MF_Main::getInstance()->Cover->getById($id)->file ? '0' : '1'; 
		$this->_helper->layout->disableLayout();
	}
	
}